package com.eoner.managerlibrary.version;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.eoner.commonbean.config.SystemVersionBean;
import com.eoner.common.config.Config;
import com.eoner.baselib.utils.notify.RxBus;
import com.eoner.commonbean.config.SystemConfigBean;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.managerlibrary.R;

/**
 * 作者：sean
 * 创建时间：2017/6/12 10:46
 * 功能说明：强制更新提示
 */

public class UpdateDialog extends Dialog {

    private TextView tvVersionName;
    private TextView tvUpdateMessage;
    private ImageView ivClose;
    private TextView tvUpdate;

    private Context context;
    private boolean isHiddenCancle;
    private RxBus rxBus;

    public UpdateDialog(@NonNull Context context) {
        super(context, R.style.MyDialog);
        this.context = context;
    }

    public UpdateDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCanceledOnTouchOutside(false);
        setContentView(R.layout.dialog_update);
        tvVersionName = findViewById(R.id.tv_version_name);
        tvUpdateMessage = findViewById(R.id.tv_update_message);
        ivClose = findViewById(R.id.iv_close);
        tvUpdate = findViewById(R.id.tv_update);

        rxBus = RxBus.$();

        if(isHiddenCancle){
            ivClose.setVisibility(View.GONE);
        }else {
            ivClose.setVisibility(View.VISIBLE);
        }

        ivClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        tvUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(optionClickListerner!=null){
                    optionClickListerner.update();
                }
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK&&isHiddenCancle) {
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (!isHiddenCancle){
            rxBus.post(Config.UPDATE_DIALOG_DISMISS,context.getClass().getSimpleName());
        }
    }

    public void setUpdateInfo(@NonNull SystemVersionBean bean){
        tvVersionName.setText(bean.getVersion_number());
        tvVersionName.setVisibility(View.VISIBLE);
        tvUpdateMessage.setText(bean.getIntroduce().replace("\\n","\n"));
    }


    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        window.setWindowAnimations(R.style.bottomShow);
        WindowManager.LayoutParams windowparams = window.getAttributes();
        windowparams.width = ScreenUtils.getScreenWidth() - ScreenUtils.dp2px(32);
        window.setGravity(Gravity.CENTER);
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(windowparams);
    }

    private OptionClickListerner optionClickListerner;

    public void setOptionClickListerner(OptionClickListerner optionClickListerner) {
        this.optionClickListerner = optionClickListerner;
    }

    public interface OptionClickListerner{
        public void cancle();
        public void update();
    }

    public void hiddenCancel(boolean hidden){
            this.isHiddenCancle = hidden;
    }
}
